package com.rul.mars.product.controller;

import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.ProductCategoryNode;
import com.rul.mars.mbg.domain.ProductCategoryParam;
import com.rul.mars.product.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员商品分类服务
 *
 * @author RuL
 */
@Api(tags = "AdminProductCategoryController", description = "管理员商品分类服务")
@RestController
@RequestMapping("/admin/category")
public class AdminProductCategoryController {

    @Autowired
    private ProductCategoryService categoryService;


    /**
     * 创建分类
     *
     * @param param 创建商品分类所需参数
     * @return 创建结果
     */
    @ApiOperation("创建商品分类")
    @PostMapping("/create")
    public CommonResult createCategory(@RequestBody ProductCategoryParam param) {
        boolean success = categoryService.createCategory(param);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除商品分类
     *
     * @param id 分类ID
     * @return 删除结果
     */
    @ApiOperation("删除商品分类")
    @PostMapping
    public CommonResult deleteCategory(@RequestParam("id") Long id) {
        boolean success = categoryService.deleteCategory(id);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改商品分类信息
     *
     * @param id    分类ID
     * @param param 分类修改参数
     * @return 修改结果
     */
    @ApiOperation("修改商品分类信息")
    @PostMapping("/update/{id}")
    public CommonResult updateCategory(@PathVariable("id") Long id, @RequestBody ProductCategoryParam param) {
        boolean success = categoryService.updateCategory(id, param);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 根据ID获取分类信息及其子分类信息
     *
     * @param id 分类ID
     * @return 分类信息
     */
    @ApiOperation("根据ID获取分类信息及其子分类信息")
    @GetMapping("/info/{id}")
    public CommonResult<ProductCategoryNode> getCategoryInfo(@PathVariable("id") Long id) {
        ProductCategoryNode category = categoryService.getInfo(id);
        return CommonResult.success(category);
    }
}
